package com.tarena.dang.daos.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.tarena.dang.daos.AddressDAO;
import com.tarena.dang.pojos.Address;
import com.tarena.dang.utils.DBUtils;

public class JdbcAddressDAO implements AddressDAO {
	private static String findAddressByUserId = "select * from d_receive_address where user_id=?";
	private static String addAddress = "insert into d_receive_address "
			+ "(user_id,receive_name,full_address,postal_code,mobile,phone) "
			+ "values(?,?,?,?,?,?)";
	private static String findAddressById = "select * from d_receive_address where id=?";
	private static String updateAddressById = "update d_receive_address "
			+ "set user_id=?,receive_name=?,full_address=?,"
			+ "postal_code=?,mobile=?,phone=? " + "where id=?";

	public void updateAddressById(Address address) throws SQLException {
		PreparedStatement pst = DBUtils.getConnection().prepareStatement(
				updateAddressById);
		pst.setInt(1, address.getUserId());
		pst.setString(2, address.getReceiveName());
		pst.setString(3, address.getFullAddress());
		pst.setString(4, address.getPostalCode());
		pst.setString(5, address.getMobile());
		pst.setString(6, address.getPhone());
		pst.setInt(7, address.getId());
		pst.executeUpdate();
		DBUtils.closeConnection();
	}

	public List<Address> findAddressByUserId(int userId) throws SQLException {
		PreparedStatement pst = DBUtils.getConnection().prepareStatement(
				findAddressByUserId);
		pst.setInt(1, userId);
		ResultSet rs = pst.executeQuery();
		List<Address> addresses = new ArrayList<Address>();
		while (rs.next()) {
			Address a = new Address();
			a.setId(rs.getInt("id"));
			a.setUserId(rs.getInt("user_id"));
			a.setReceiveName(rs.getString("receive_name"));
			a.setFullAddress(rs.getString("full_address"));
			a.setPostalCode(rs.getString("postal_code"));
			a.setMobile(rs.getString("mobile"));
			a.setPhone(rs.getString("phone"));
			addresses.add(a);
		}
		DBUtils.closeConnection();
		return addresses;
	}

	public Address findAddressById(int aid) throws SQLException {
		PreparedStatement pst = DBUtils.getConnection().prepareStatement(
				findAddressById);
		pst.setInt(1, aid);
		ResultSet rs = pst.executeQuery();
		rs.next();
		Address a = new Address();
		a.setId(rs.getInt("id"));
		a.setUserId(rs.getInt("user_id"));
		a.setReceiveName(rs.getString("receive_name"));
		a.setFullAddress(rs.getString("full_address"));
		a.setPostalCode(rs.getString("postal_code"));
		a.setMobile(rs.getString("mobile"));
		a.setPhone(rs.getString("phone"));
		DBUtils.closeConnection();
		return a;
	}

	public void addAddress(Address address) throws SQLException {
		PreparedStatement pst = DBUtils.getConnection().prepareStatement(
				addAddress, Statement.RETURN_GENERATED_KEYS);
		pst.setInt(1, address.getUserId());
		pst.setString(2, address.getReceiveName());
		pst.setString(3, address.getFullAddress());
		pst.setString(4, address.getPostalCode());
		pst.setString(5, address.getMobile());
		pst.setString(6, address.getPhone());
		pst.executeUpdate();
		ResultSet rs = pst.getGeneratedKeys();
		rs.next();
		address.setId(rs.getInt(1));

		DBUtils.closeConnection();
	}

}
